package com.youdoneed.community.forum.service;

import com.github.pagehelper.PageHelper;
import com.youdoneed.base.application.AppException;
import com.youdoneed.base.application.RequestMessage;
import com.youdoneed.community.common.utils.StringUUIdUtil;
import com.youdoneed.community.forum.dao.ForummessageMapper;
import com.youdoneed.community.forum.dao.ForumnoticeMapper;
import com.youdoneed.community.forum.dao.ForumreportMapper;
import com.youdoneed.community.forum.entity.Forumcomment;
import com.youdoneed.community.forum.entity.Forummessage;
import com.youdoneed.community.forum.entity.Forumnotice;
import com.youdoneed.community.forum.entity.Forumreport;
import com.youdoneed.community.forum.vo.ForumnoticeVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 通知模块服务接口
 *
 * @author liudong
 * @date 2017.7.12
 */
@Service
public class ForumNoticeService {

    private final Logger logger = LoggerFactory.getLogger(ForumNoticeService.class);

    @Autowired
    ForumnoticeMapper forumnoticeMapper;


    /**
     * 功能描述:根据主键id删除
     *
     * @param forumNoticeId 主键id
     */
    @Transactional
    public int deleteByPrimaryKey(String forumNoticeId) {
        if (StringUtils.isEmpty(forumNoticeId)) {
            throw new AppException(RequestMessage.getMessage("NoticeIdNull"));
        }
        return forumnoticeMapper.deleteByPrimaryKey(forumNoticeId);
    }

    @Transactional
    public int deleteBySourceId(String sourceId) {
        if (StringUtils.isEmpty(sourceId)) {
            throw new AppException(RequestMessage.getMessage("sourceId is Null"));
        }
        return forumnoticeMapper.deleteBySourceId(sourceId);
    }


    /**
     * 功能描述：插入消息通知数据，主键需要手动生成
     */
    @Transactional
    public int insertSelective(Forumnotice record) {
        if (record == null) {
            throw new AppException(RequestMessage.getMessage("NoticeIsNull"));
        }
        if (record.getCreateDate() == null)
            record.setCreateDate(new Date());
        if (!StringUtils.hasText(record.getForumNoticeId()))
            record.setForumNoticeId(StringUUIdUtil.getUUID());
        if (record.getReadStatus() == null)
            record.setReadStatus(0);
        return forumnoticeMapper.insertSelective(record);
    }

    /**
     * 功能描述:根据主键id查询
     *
     * @param forumNoticeId 主键id
     */
    public Forumnotice selectByPrimaryKey(String forumNoticeId) {
        if (StringUtils.isEmpty(forumNoticeId)) {
            throw new AppException(RequestMessage.getMessage("NoticeIdNull"));
        }
        return forumnoticeMapper.selectByPrimaryKey(forumNoticeId);
    }

    /**
     * 功能描述:根据主键id更新数据
     */
    @Transactional
    public int updateByPrimaryKeySelective(Forumnotice record) {
        return forumnoticeMapper.updateByPrimaryKeySelective(record);
    }


    /**
     * 功能描述:根据条件查询通知列表
     *
     * @param userId     用户id  where做了非空判断
     * @param qrySdate   查询开始时间 where做了非空判断
     * @param qryEdate   查询结束时间 where做了非空判断
     * @param readStatus 消息状态 where做了非空判断
     */
    public List<Forumnotice> qryNoticeList(String userId, String qrySdate, String qryEdate, Integer readStatus, Integer currentPage, Integer rows) {
        try {
            PageHelper.startPage(currentPage, rows);
            return forumnoticeMapper.qryNoticeList(userId, qrySdate, qryEdate, readStatus);
        } catch (Exception e) {
            logger.info("获取评论失败" + e);
            return null;
        }

    }

    /**
     * 功能描述:根据条件查询通知列表数量
     *
     * @param userId     用户id  where做了非空判断
     * @param qrySdate   查询开始时间 where做了非空判断
     * @param qryEdate   查询结束时间 where做了非空判断
     * @param readStatus 消息状态 where做了非空判断
     */
    public int qryNoticeCnt(String userId, String qrySdate, String qryEdate, Integer readStatus) {
        return forumnoticeMapper.qryNoticeCnt(userId, qrySdate, qryEdate, readStatus);
    }

    /**
     * 功能描述:根据条件查询最新通知，按时间倒序
     *
     * @param userId     用户id  where做了非空判断
     * @param qrySdate   查询开始时间 where做了非空判断
     * @param readStatus 消息状态 where做了非空判断
     */
    public ForumnoticeVo qryFirstNotice(String userId, String qrySdate, Integer readStatus) {
        return forumnoticeMapper.qryFirstNotice(userId, qrySdate, readStatus);
    }

    /**
     * 功能描述:根据条件更新通知状态
     *
     * @param userId        用户id  where做了非空判断
     * @param forumNoticeId 消息通知主键id where做了非空判断
     * @param qrySdate      查询开始时间 where做了非空判断
     * @param qryEdate      查询结束时间 where做了非空判断
     * @param readStatus    消息状态 where做了非空判断
     */
    @Transactional
    public int updateNoticeList(String userId, String forumNoticeId, String qrySdate, String qryEdate, Integer readStatus) {
        return forumnoticeMapper.updateNoticeList(userId, forumNoticeId, qrySdate, qryEdate, readStatus);
    }

}
